<?php
use yii\helpers\Url;
?>

<style type="text/css">
   .tab{

   }
   .tab tr td{
       height:30px;
       line-height: 30px;
   }
</style>

<div>
    <br/>

    <form method="POST" action="<?php echo Url::toRoute(['mtp/myscrm-organization/save-contract']) ?>"
        onsubmit="return check_form(this)"
        >
        <input type="hidden" value="<?php echo $orgId ?>" name="organization_id">
        <input type="hidden" value="" name="auth_info" id="auth_info">

    <table border="1" style="border-collapse:collapse" width="60%" class="tab">
        <tr>
            <td>
               合同编号:
            </td>
            <td>
                <input type="text" name="contract_num" />
            </td>
        </tr>
        <tr>
            <td>
                合同甲方:
            </td>
            <td>
                <input type="text" name="party_a" />
            </td>
        </tr>
        <tr>
            <td>
                购买类型:
            </td>
            <td>
                <label><input type="radio" name="contract_type" value="1" checked />新增</label>
                <label><input type="radio" name="contract_type" value="2" />增购</label>
            </td>
        </tr>
        <!--
 if(!array_key_exists('contract_num', $post) || !array_key_exists('party_a', $post) ||
        !array_key_exists('contract_type', $post) || !array_key_exists('begin_on', $post) ||
        !array_key_exists('end_on', $post) || !array_key_exists('organization_id', $post) ||
        !array_key_exists('auth_info', $post)){
        return $this->json(["retCode" => -1, "result" => false, "msg" => "参数不能为空"]);
    }
 -->
        <tr>
            <td>
                租期:
            </td>
            <td>
                <input type="text" name="begin_on" value="" checked /> 至
                <input type="text" name="end_on" value="" />
            </td>
        </tr>
    </table>
        <div style="border-bottom: 2px solid #357ebd;margin: 10px 0;padding-bottom: 4px;">授权信息</div>
    <table border="1" style="border-collapse:collapse" width="50%" id="auth-tab">
        <tr>
            <th>ID</th>
            <th>套餐名称</th>
            <th>授权项目</th>
            <th>已用授权</th>
        </tr>
        <?php
        foreach($pkgs as $pkg){?>
            <tr>
                <td style="text-align: center;"><?php echo $pkg['id'] ?></td>
                <td style="text-align: center;"><?php echo $pkg['friendly_name'] ?></td>
                <td style="text-align: center;"><input type="text" value="" size="4"></td>
                <td style="text-align: center;"><input type="text" value="" size="4"></td>
            </tr>
        <?php
        }
        ?>
    </table>
        <div>
            <input type="submit" value=" 提交 ">
        </div>
    </form>

</div>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">

    function setAuthInfo(){
        var authinfo = $("#auth_info");
        var tds = $("#auth-tab").find("tr");
        var authinfoVal = [];
        $.each(tds, function(i, o){
            var ao = {};
            var ttds = $(o).find("td");
            if(ttds[0]){
                ao['package_id'] = $(ttds[0]).html();
            }
            if(ttds[2]){
                var inputs = $(ttds[2]).find("input");
                if(inputs){
                    ao['total_num'] = parseInt($(inputs[0]).val());
                }
            }
            if(ttds[3]){
                var inputs = $(ttds[3]).find("input");
                if(inputs){
                    ao['used_num'] = parseInt($(inputs[0]).val());
                }
            }
            if(Object.keys(ao).length){
                authinfoVal[authinfoVal.length] = ao;
            }
        });
        $("#auth_info").val(JSON.stringify(authinfoVal));
    }
    function check_form($oform){
        setAuthInfo();
        return true;
    }
</script>